package com.example.hehecat.yuejian1.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.ActivityListContract;
import com.example.hehecat.yuejian1.mvp.model.bean.ActivityBean;
import com.example.hehecat.yuejian1.mvp.presenter.ActivityListPresenter;
import com.example.hehecat.yuejian1.ui.adapter.ActivityListAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class ActivityListFragment extends Fragment implements ActivityListContract.View {

    @BindView(R.id.activity_list)
    RecyclerView activityList;
    Unbinder unbinder;

    private ActivityListAdapter adapter;
    private ActivityListPresenter presenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity, container, false);
        unbinder = ButterKnife.bind(this, view);

        //创建布局管理
        final RecyclerView recyclerView = view.findViewById(R.id.activity_list);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new MyItemDecoration());

        //创建适配器
        adapter =new ActivityListAdapter(R.layout.item_list,getContext());

        //给RecyclerView设置适配器
        presenter = new ActivityListPresenter(this);
        presenter.updateAdapterData();
        recyclerView.setAdapter(adapter);
        return view;
    }



    public static ActivityListFragment newInstance(String name) {
        ActivityListFragment fragment = new ActivityListFragment();
        Bundle args = new Bundle();
        args.putString("name", name);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void updateAdapterData(List<ActivityBean> data) {
        adapter.addData(data);
    }
}
